/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-03-20     weihui       the first version
 */
#include "bsp_svpwm.h"
#include "Pid/pid.h"
#include "App/Bldc_Motor.h"
#include <Bsp/bsp_adc.h>
extern  TIM_HandleTypeDef htim1;
extern  ADC_HandleTypeDef hadc1;
//extern  void MX_TIM1_Init(void);

void svpwm_init(void)
{
    rt_pin_mode(Motor_enable_pin, PIN_MODE_OUTPUT);
    Motor_Disable();

    MX_TIM1_Init();
//
    HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);
    HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_2);
    HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_3);
    HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_4);
//
    HAL_TIMEx_PWMN_Start(&htim1,TIM_CHANNEL_1);
    HAL_TIMEx_PWMN_Start(&htim1,TIM_CHANNEL_2);
    HAL_TIMEx_PWMN_Start(&htim1,TIM_CHANNEL_3);
//
//
    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1,0);
    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2,0);
    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3,0);
    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_4,2650);


    Motor_Enable();

    rt_thread_mdelay(50);
    bsp_adc_init();

    __HAL_ADC_ENABLE_IT(&hadc1, ADC_IT_JEOC);
//    __HAL_TIM_ENABLE(&htim1);
    HAL_ADCEx_InjectedStart(&hadc1);
    __HAL_DBGMCU_FREEZE_TIM1();


    PID_Init();
    Start_Up();
}


void TIM1_UP_TIM10_IRQHandler(void)
{
    /* USER CODE BEGIN TIM1_UP_TIM10_IRQn 0 */
    rt_interrupt_enter();
    /* USER CODE END TIM1_UP_TIM10_IRQn 0 */
    HAL_TIM_IRQHandler(&htim1);
    /* USER CODE BEGIN TIM1_UP_TIM10_IRQn 1 */
    rt_interrupt_leave();
    /* USER CODE END TIM1_UP_TIM10_IRQn 1 */
}


static void shut_pwm(void)
{
    HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_1);
    HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_2);
    HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_3);
//
    HAL_TIMEx_PWMN_Stop(&htim1,TIM_CHANNEL_1);
    HAL_TIMEx_PWMN_Stop(&htim1,TIM_CHANNEL_2);
    HAL_TIMEx_PWMN_Stop(&htim1,TIM_CHANNEL_3);
}


static int pwm_channel_ctr(int argc, char *argv[])
{
    unsigned char pwm_channel =  atoi(argv[1]);
    switch(pwm_channel)
    {
        case 1:
            __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1,atoi(argv[3]));
            if(!strcmp("open",argv[2])){
                HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);HAL_TIMEx_PWMN_Start(&htim1,TIM_CHANNEL_1);
            }
            else {
                HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_1);HAL_TIMEx_PWMN_Stop(&htim1,TIM_CHANNEL_1);
            }

            break;
        case 2:
            __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2,atoi(argv[3]));
            if(!strcmp("open",argv[2])){
                HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_2);HAL_TIMEx_PWMN_Start(&htim1,TIM_CHANNEL_2);
            }
            else {
                HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_2);HAL_TIMEx_PWMN_Stop(&htim1,TIM_CHANNEL_2);
            }
            break;
        case 3:
            __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3,atoi(argv[3]));
            if(!strcmp("open",argv[2])){
                HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_3);HAL_TIMEx_PWMN_Start(&htim1,TIM_CHANNEL_3);
            }
            else {
                HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_3);HAL_TIMEx_PWMN_Stop(&htim1,TIM_CHANNEL_3);
            }
            break;
    }


    rt_kprintf("pwm_channel:%d,status:%s,pwm_duty:%d\n",pwm_channel,argv[2],atoi(argv[3]));
    return RT_EOK;
}
MSH_CMD_EXPORT(pwm_channel_ctr, num1:通道设置1-2-3  num2:opne-close  num3:脉宽);
